<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	cfc('core')->checklogin();
	$yewuid=$_RQ['yewuid'];
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('realname','nickname','mobile'));
	if($yewuid){
		$yewu=pdo_get('jy_daibao_yewu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$yewuid),array('id','title','fee','image','desc','type'));
	}else{
		exi('请先选业务！','error');
	}
	$post=array(
		'yewuid'=>$yewuid,
		'fee'=>$yewu['fee'],
		'type'=>$yewu['type'],
		'name'=>$member['realname']?:$member['nickname'],
		'mobile'=>$member['mobile'],
	);
	$return['yewu']=$yewu?:array();
	$post['day']=date('Y-m-d');
	$post['time']=date('H:i',time()+1800);
	if($yewu['type']==1){
		$post['year']=intval(date('Y',time()))-1;
	}
	$return['post']=$post;
	$return['mymerchs']=pdo_getall('jy_daibao_merch',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['buyxieyi']=$this->plugin_setting('xieyi');
	set_html('title','下单');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$yewuid = $postdata['yewuid'];
	$merchid = $postdata['merchid'];
	$buyxieyi=$this->plugin_setting('xieyi');
	if(!$postdata['checked'] && $buyxieyi['isopen']){
			//exi('请先阅读协议！','error');
	} 
	if(!$postdata['name']){
			exi('请输入联系人姓名','error');
	}
	if(!$postdata['mobile']){
			exi('请输入联系人手机号码','error');
	}  
	if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  	 
			exi('请输入正确的手机号码','error');
	}
	if(!$merchid){
		exi('请选代办的企业！','error');
	}
	if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
	}
	$postdata['address']=$postdata['address']?:$postdata['addressinfo']['address'];
	//判断待支付订单及判断是否超过可预订次数
	$serve=pdo_get('jy_daibao_yewu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$yewuid));
	$totalprice=$serve['fee'];
	$merchinfo=pdo_get('jy_daibao_merch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$merchid));
	$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'yewuid'=>$yewuid,
			'merchid'=>$merchid,
			'type'=>$postdata['type'],
			'year'=>$postdata['year'],
			'fee'=>$totalprice,
			'address'=>$postdata['address'],
			'name'=>$postdata['name'],
			'mobile'=>$postdata['mobile'],
			'sn'=>get_ordersn('jy_daibao_order','sn'),
			'remark'=>$postdata['remark'],
			'merchinfo'=>ijson_encode($merchinfo),
			'fileds'=>ijson_encode($postdata['fileds']),
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_daibao_order',$insert);
		$id=pdo_insertid();
		if($totalprice>0){
			$params=array(
					'id'=>$id,
					'title'=>'工商代办订单',
					'fee'=>$totalprice,
					'plugin'=>'jy_daibao',
					//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
					'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
					'back_url'=>'jy_daibao/uporder/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
					'success_url'=>'jy_daibao/order/',
					'error_url'=>'jy_daibao/order/',
			);
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
		}else{
			$this->pdo_update('order',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			exi('预约成功！','','jy_daibao/order/');
		}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_daibao_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_daibao_order',array('status'=>1,'paytype'=>$payresult['pay_type']),array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('model')->jy_commission($order);
	
	exi('支付成功','success','jy_daibao/order/list/status=1&id='.$order['id']);
}